package ConnDB;

import ConnDB.ConnODBC;
import java.sql.*;
import java.util.*;

public class ConnTurnPage{
  
		private ConnODBC conn=null;
		private ResultSet rs=null;
		
		private long l_totalpage=0;
		private	long l_totalnum=0; 
		private	long l_curpage=0;
		private long l_start=0;
		private long l_end=0;
		
  		public ConnTurnPage()
		{
		
     		try
		   {
					conn=new ConnODBC();
			}
    		catch (Exception e)
		   {
					System.err.println(e.getMessage());
			}
		}
		public void closeConn() throws SQLException 
		{
		  if(conn==null) return;
		  conn.closeConnection();
		}
		public Enumeration getData(String sql,int pageSize,int nowPage)
		{
		    Vector dataEnu=new Vector();
	        l_totalnum=0;
			
			try
			{
            String fn_Str="";
				String fd_Str="";
				String columnStr="";
				String tempStr="";
   			int numberOfColumns=0;
				int ft_arr[];
				
				rs=conn.executeQuery(sql);
				System.out.print(sql);
                while(rs.next())
                {
                    l_totalnum++;
                }
            if(l_totalnum==0) return null;
				getPosition(pageSize,nowPage);
							
				ResultSetMetaData rsmd = rs.getMetaData();
                numberOfColumns = rsmd.getColumnCount();
				
				ft_arr=new int[numberOfColumns+1];
				
                for(int i=1;i<=numberOfColumns;i++)
				{
				  if(i!=numberOfColumns)
				  {
				    fn_Str+=rsmd.getColumnName(i)+",";
				  }
				  else
				  {
				    fn_Str+=rsmd.getColumnName(i);
				  }
				  ft_arr[i]=rsmd.getColumnType(i);
				}
				
				//dataEnu.addElement(fn_Str);
				
				while(l_start<l_end) 
                { 
                    rs.absolute((int)l_start+1);
            
				    for(int i=1;i<=numberOfColumns;i++)
					{
					 if(rs.getObject(i)==null) 
					 {
					     columnStr="&nbsp;";
					 }
					 else
					 {
					  switch(ft_arr[i])
					  {
					
					   case Types.BIT:
						     columnStr=rs.getString(i);break;
						case Types.CHAR:
						     columnStr=rs.getString(i);break;
						case 93: //Types.DATE:
						     columnStr=rs.getDate(i).toString();break;
						case Types.DECIMAL:
						     columnStr=rs.getString(i);break;
						case Types.FLOAT:
						     columnStr=new Float(rs.getFloat(i)).toString();break;
						//int
						case Types.INTEGER:
						     columnStr=new Integer(rs.getInt(i)).toString();break;
						case Types.NUMERIC:
						     columnStr=rs.getString(i);break;
						//text
						case -1:
						     columnStr=rs.getString(i);break;
						case Types.VARCHAR:
						     columnStr=rs.getString(i);break;
						default:columnStr="";
						}
				     }//end else
				     if(columnStr.equals("")) columnStr="&nbsp;";
				      if(i!=numberOfColumns)
				      {
				         tempStr+=columnStr.trim()+",";
				      }
				      else
				      {
				         tempStr+=columnStr.trim();
				       }
				    }//end for
					dataEnu.addElement(tempStr);
					tempStr="";
					l_start++; 
				} 
			}
			catch(SQLException ex)
			{
				System.err.println("aq.executeQuery:"+ex.getMessage());
				dataEnu=null;
			}
			if(dataEnu==null)
			{
			   return null;
			}
			else
			{
			   return dataEnu.elements();
			}	
		}
        private void getPosition(int pageSize,int nowPage)
		{
		    if(nowPage>=1 && pageSize>=1 && l_totalnum>=1) 
            {
               l_totalpage=(long)Math.ceil((double)l_totalnum/(double)pageSize);
			      
               if(nowPage>=l_totalpage) 
                  l_curpage = l_totalpage; 
               else 
                  l_curpage = nowPage; 
            } 
            else 
            { 
               l_totalpage=0;
               return;
            } 
            
			l_start = (l_curpage-1) * pageSize; 
            l_end = l_start + pageSize; 
            
			if (l_end > l_totalnum) 
                l_end = l_totalnum; 
		}
		public long getPages()
		{
		  return l_totalpage;
		}
		public long getRecordCount()
		{
		  return l_totalnum;
		}
		public long getCurrentPage()
		{
		  return l_curpage;
		}
		public long getPrepage() 
        { 
          if (l_curpage-1>=0) 
          { 
            return l_curpage-1; 
          } 
          else 
          { 
            return 0; 
          } 
        } 
        public long getNextpage() 
        { 
          if (l_curpage+1<=l_totalpage) 
          { 
             return l_curpage+1; 
          } 
          else 
          { 
            return l_totalpage; 
          } 
        }
     }
